package com.gis_luq.lib.Draw.Util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.zip.ZipFile;

public class WriteMsgToApkUtils
{

	/**
	 * 读取Android安装包.apk文件末尾的数据
	 * 
	 * @param file
	 *            .apk文件
	 * @return
	 */

	public static String readApk(File file) {
		byte[] bytes = null;
		RandomAccessFile accessFile = null;
		try {
			accessFile = new RandomAccessFile(file, "r");
			long index = accessFile.length();

			bytes = new byte[2];
			index = index - bytes.length;
			accessFile.seek(index);
			accessFile.readFully(bytes);

			int contentLength = stream2Short(bytes, 0);

			bytes = new byte[contentLength];
			index = index - bytes.length;
			accessFile.seek(index);
			accessFile.readFully(bytes);

			return new String(bytes, "utf-8");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (accessFile != null) {
					accessFile.close();
				}
			} catch (Exception e) {

			}
		}
		return null;
	}


	/**
	 * 将信息写入Android安装包.apk文件末尾
	 * 
	 * @param file 要写入信息的.apk文件
	 * @param comment 写入的信息
	 */
	public static void writeApk(File file, byte[] byteComment)
	{
		ZipFile zipFile = null;
		ByteArrayOutputStream outputStream = null;
		RandomAccessFile accessFile = null;
		try
		{
			zipFile = new ZipFile(file);
//			String zipComment = zipFile.getComment();
//			if (zipComment != null)
//			{
//			}
			
			int c = byteComment.length;

			outputStream = new ByteArrayOutputStream();

			outputStream.write(byteComment);
			outputStream.write(short2Stream((short) byteComment.length));

			byte[] data = outputStream.toByteArray();

			accessFile = new RandomAccessFile(file, "rw");
			accessFile.seek(file.length() - 2);
			accessFile.write(short2Stream((short) data.length));
			accessFile.write(data);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (zipFile != null)
				{
					zipFile.close();
				}
				if (outputStream != null)
				{
					outputStream.close();
				}
				if (accessFile != null)
				{
					accessFile.close();
				}
			}
			catch (Exception e)
			{

			}

		}
	}
	
	/**
	 * short转换成字节数组（小端序）
	 * 
	 * @param data
	 * @return
	 */
	private static short stream2Short(byte[] stream, int offset)
	{
		ByteBuffer buffer = ByteBuffer.allocate(2);
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		buffer.put(stream[offset]);
		buffer.put(stream[offset + 1]);
		return buffer.getShort(0);
	}

	/**
	 * 字节数组转换成short（小端序）
	 * 
	 * @param stream
	 * @param offset
	 * @return
	 */
	private static byte[] short2Stream(short data)
	{
		ByteBuffer buffer = ByteBuffer.allocate(2);
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		buffer.putShort(data);
		buffer.flip();
		return buffer.array();
	}
}
